/*
    包含多个接受组件通知触发mutation调用间接更新状态的方法的对象
*/
import {RECEIVE_ADDRESS, RECEIVE_FOODTYPE,
  RECEIVE_SHOPLIST, RECEIVE_USER_INFO,
  REMOVE_USER_INFO, RECEIVE_SHOPINFO,
  RECEIVE_SHOPRATINGS, RECEIVE_SHOPGOODS,
  INCREMENT_FOOD_COUNT, DECREMENT_FOOD_COUNT,
  ADD_FOOD_TO_CART, EMPTY_CART_FOODS,
  REMOVE_FOOD_TOCART, RECEIVE_SEARCH_SHOPLIST,
  RECEIVE_FOOD_IN_ORDER} from './mutations-types'

import {getAddress, getFoodType,
  getShopList, getUserInfo,
  logout, getShopGoods,
  getShopInfo, getShopRatings,
  searchShop} from '../api'

export default {
  // 异步请求获取地址
  async reqAddress ({commit, state}) {
    // 拼接纬度经度做参数传递给getAddress方法
    const geohash = state.latitude + ',' + state.longitude
    const result = await getAddress(geohash)
    if (result.code === 0) { // 请求成功，响应体的code属性值为0
      const address = result.data
      commit(RECEIVE_ADDRESS, {address})
    }
  },

  // 异步请求食品类型
  async reqFoodType ({commit}) {
    const result = await getFoodType()
    if (result.code === 0) {
      const foodType = result.data
      commit(RECEIVE_FOODTYPE, {foodType})
    }
  },

  // 异步请求商家列表
  async reqShopList ({commit, state}) {
    // 解构赋值
    const {latitude, longitude} = state
    const result = await getShopList(latitude, longitude)
    if (result.code === 0) {
      const shopList = result.data
      commit(RECEIVE_SHOPLIST, {shopList})
    }
  },

  // 同步记录用户信息 (当用户登录后接收用户信息，属于同步)
  recordUserInfo ({commit}, userInfo) {
    commit(RECEIVE_USER_INFO, {userInfo})
  },

  // 异步获取用户信息
  async reqUserInfo ({commit}) {
    const result = await getUserInfo()
    if (result.code === 0) { // 获取信息成功后
      const userInfo = result.data
      commit(RECEIVE_USER_INFO, {userInfo})
    }
  },

  // 异步删除用户信息
  async removeUserInfo ({commit}) {
    const result = await logout()
    if (result.code === 0) {
      commit(REMOVE_USER_INFO)
    }
  },

  // 异步获取商家商品
  async reqShopGoods ({commit}, callback) {
    const result = await getShopGoods()
    if (result.code === 0) {
      const shopGoods = result.data
      commit(RECEIVE_SHOPGOODS, {shopGoods})
      // 此时数据已经更新，在执行callback函数内语句
      callback && callback() // 当调用此actions时传了callback则调用callback，没传就不调用防止报错
    }
  },

  // 异步获取商家评价
  async reqShopRatings ({commit}, callback) {
    const result = await getShopRatings()
    if (result.code === 0) {
      const shopRatings = result.data
      commit(RECEIVE_SHOPRATINGS, {shopRatings})
      // 此时数据已经更新，在执行callback函数内语句
      callback && callback()
    }
  },

  // 异步获取商家信息
  async reqShopInfo ({commit}) {
    const result = await getShopInfo()
    if (result.code === 0) {
      const shopInfo = result.data
      commit(RECEIVE_SHOPINFO, {shopInfo})
    }
  },

  // 获取数据后操作，同步。 改变食物数量
  deleteFoodCount ({commit}, {food, isAdd}) {
    if (isAdd) {
      commit(INCREMENT_FOOD_COUNT, {food})
    } else {
      commit(DECREMENT_FOOD_COUNT, {food})
    }
  },

  // 添加food进购物车，同步
  addFoodToCart ({commit}, {food}) {
    commit(ADD_FOOD_TO_CART, {food})
  },

  // 当该food的count值小于等于0，从cartFoods中移除该食物
  removeFoodToCart ({commit, state}, {food}) {
    // 判断当前食物的count值
    if (food.count <= 0) {
      // 获取当前food下标
      const {cartFoods} = state
      let index = cartFoods.indexOf(food)
      commit(REMOVE_FOOD_TOCART, {index})
    }
  },

  // 清空购物车
  emptyCartFoods ({commit}) {
    commit(EMPTY_CART_FOODS)
  },

  // 关键字检索对应商家列表
  async searchShopList ({commit, state}, keyword) {
    const geohash = state.latitude + ',' + state.longitude
    const result = await searchShop(keyword, geohash)
    if (result.code === 0) {
      const shopList = result.data
      commit(RECEIVE_SEARCH_SHOPLIST, {shopList})
    }
  },

  // 商品结算时存放购物车内食物进订单列表
  receiveFoodsInOrder ({commit}, {foods, shopinfo}) {
    commit(RECEIVE_FOOD_IN_ORDER, {foods, shopinfo})
  }
}
